﻿#region - Class details -
/*  
 *  Developer   : Wicus Botha
 *  Description : 
 *  History     : WB - 2009-12-0 - Created
 */
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace IQ.DbA
{
    public class ServerConnections
    {
        private static readonly ServerConnections instance = new ServerConnections();

		#region # Constructor # 
        private ServerConnections()
        {

        }
        #endregion
        
        #region - Private variables -
        private Hashtable ht_ServerList = new Hashtable();
        private const string conFileName = "sqla.cz";
        #endregion

        #region - Private methods -
        private void saveServers()
        {
            GZipStream gzOut = new GZipStream(File.Create(conFileName), CompressionMode.Compress);
            StreamWriter sw = new StreamWriter(gzOut);
            foreach (string k in ht_ServerList.Keys)
            {
                sw.WriteLine(k + "Î" + ht_ServerList[k]);
            }
            sw.Flush();
            sw.Close();
            gzOut.Close();
        }
        #endregion

        #region - Public properties / variables -
        public static ServerConnections Instance
        {
            get { return instance; }
        }
        public Hashtable ServerList
        {
            get { return ht_ServerList; }
        }
        #endregion

        #region - Public methods -
        public void LoadServers()
        {
            if (System.IO.File.Exists(conFileName))
            {
                GZipStream gzOut = new GZipStream(File.OpenRead(conFileName), CompressionMode.Decompress);
                StreamReader sr = new StreamReader(gzOut);
                ht_ServerList.Clear();
                string tmp = "";
                while ((tmp = sr.ReadLine()) != null)
                {
                    string[] sc = tmp.Split('Î');
                    ht_ServerList.Add(sc[0], sc[1]);
                }
                sr.Close();
                gzOut.Close();
            }
        }
        public void RemoveSever(string server)
        {
            if (ht_ServerList.ContainsKey(server))
            {
                ht_ServerList.Remove(server);
                saveServers();
            }
        }
        public void SetServerConnectionStr(string server, string conString)
        {
            if (ht_ServerList.ContainsKey(server))
            {
                ht_ServerList[server] = conString;
            }
            else
            {
                ht_ServerList.Add(server, conString);
                saveServers();
            }
        }
        public string GetServerConnectString(string server)
        {
            if (ht_ServerList.ContainsKey(server))
            {
                return ht_ServerList[server].ToString();
            }
            else
                return "";
        }

        #endregion
    }
}
